const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

Page({
  data: {
    status: -1,
    list: [],
    page: 1,
    limit: 20,
    more: true,
    theme: app.themeData,
    abnorData: {
      imgSrc: '/common/static/images/abnor-04.png',
      text: '暂无相关数据',
    },
  },
  onLoad() {
    let isBrand = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand; 
    this.setData({
      isBrand,
    })
    this.loadList();
  },
  onReachBottom() {
    this.loadList();
  },
  switchTab(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      status,
      list: [],
      page: 1,
      more: true,
    });
    this.loadList();
  },
  loadList() {
    let that = this;
    let { status, list, page, limit, more } = that.data;
    if (more) {
      wx.showLoading({
        title: '加载中...',
      });
      app.ws_micro_shoprequest({
        url: api.adminer.applycheck,
        data: {
          status, page, limit,
        },
        success: res => {
          wx.hideLoading();
          if (res.code === 0) {
            let newlist = res.data.list;
            if (newlist.length > 0) {
              page += 1;
              that.setData({
                list: list.concat(newlist),
                page,
              });
            } else {
              that.setData({
                more: false,
              });
            }
          } else {
            wx.showToast({
              title: res.msg || '加载出错',
              icon: 'none',
            });
          }
        },
      });
    }
  },
  checkOpr(e) {
    let that = this;
    let { id, state } = e.currentTarget.dataset;
    let list = this.data.list;
    wx.showModal({
      title: '提示',
      content: '是否确定要执行该操作',
      success: function (resp) {
        if (resp.confirm) {
          app.ws_micro_shoprequest({
            url: api.adminer.checkopr,
            data: {
              id,
              status: state,
            },
            success: res => {
              if (res.code === 0) {
                for (let i in list) {
                  if (Number(id) === Number(list[i].id)) {
                    list.splice(i,1);
                    that.setData({
                      list,
                    });
                    break;
                  }
                }
              } else {
                wx.showToast({
                  title: res.msg || '操作出错',
                  icon: 'none',
                });
              }
            },
          });
        }
      },
    });
  },
});
